package forms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import models.Blog;

public class BlogForm {
	public static TreeMap checkValid(HttpServletRequest request, Blog blog) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String blogId = (String) request.getParameter("blogId");
		String blogName = (String) request.getParameter("blogName");
		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");
		String category = (String) request.getParameter("category");
		String limitSite = (String) request.getParameter("limitSite");
		String type = (String) request.getParameter("type");
		String status = (String) request.getParameter("status");
		
		String msgBlogId = "";
		if (blogId == null || blogId.equals("")) {
			if (type != null && type.equalsIgnoreCase("wordpress")) {
				msgBlogId = "Blog Url is not empty!";
			} else {
				msgBlogId = "BlogSpot ID is not empty!";
			}
			
			checkValid.put("blogId", msgBlogId);
			blog.setBlogId(blogId);
		} else {
			blog.setBlogId(blogId);
		}
		
		if (type == null || type.isEmpty()) {
			checkValid.put("type", "Type is not empty");
			//blog.setType(type);
		} else {
			blog.setType(type);
		}
		
		String msgBlogName = "";
		if (blogName == null || blogName.equals("")) {
			if (type != null && type.equalsIgnoreCase("wordpress")) {
				msgBlogName = "Blog Name is not empty!";
			} else {
				msgBlogName = "Blog Url is not empty!";
			}
			checkValid.put("blogName", msgBlogName);
			blog.setBlogName(blogName);
		} else {
			blog.setBlogName(blogName);
		}
		
		String msgUsername = "";
		if (username == null || username.equals("")) {
			if (type != null && type.equalsIgnoreCase("wordpress")) {
				msgUsername = "Blog's username is not empty!";
			} else {
				msgUsername = "Email is not empty!";
			}
			checkValid.put("username", msgUsername);
			blog.setUsername(username);
		} else {
			blog.setUsername(username);
		}
		
		String msgPassword = "";
		if (password == null || password.equals("")) {
			if (type != null && type.equalsIgnoreCase("wordpress")) {
				msgPassword = "Blog's password is not empty!";
			} else {
				msgPassword = "Password is not empty!";
			}
			checkValid.put("password", msgPassword);
			blog.setPassword(password);
		} else {
			blog.setPassword(password);
		}
		
		if (category == null || category.equals("")) {
			checkValid.put("category", "Category is not empty!");
			blog.setCategories(new ArrayList<String>());
		} else {
			String[] c = category.split(",");
			List<String> categories = new ArrayList<String>();
			for (int i = 0; i < c.length; i++) {
				categories.add(c[i]);
			}
			blog.setCategories(categories);
		}
		
		if (limitSite != null) {
			try {
				blog.setLimitSite(Integer.parseInt(limitSite));
			} catch (Exception e) {
				checkValid.put("limitSite", "Limit site not valid!");
			}
		}
		
		if (status != null) {
			try {
				blog.setStatus(Integer.parseInt(status));
			} catch (Exception e) {
				checkValid.put("status", "Status not valid!");
			}
		}
		
		form.put("checkValid", checkValid);
		form.put("blog", blog);
		return form;
	}
}
